package net.risesoft.risebi.collection.web.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.risesoft.risebi.collection.impexp.ImpExpMessage;
import net.risesoft.risebi.collection.impexp.Importer;
import net.risesoft.risebi.collection.model.ReportPeriod;
import net.risesoft.risebi.collection.model.ReportPeriodType;
import net.risesoft.risebi.collection.model.ReportWindowPeriod;
import net.risesoft.risebi.collection.model.User;
import net.risesoft.risebi.collection.service.CatalogManager;
import net.risesoft.risebi.collection.service.DbMetaDataManager;
import net.risesoft.risebi.collection.service.DynaBeanManager;
import net.risesoft.risebi.collection.service.ReportInstanceManager;
import net.risesoft.risebi.collection.service.ReportPeriodManager;
import net.risesoft.risebi.collection.service.ReportPeriodTypeManager;
import net.risesoft.risebi.collection.service.ReportWindowPeriodManager;
import net.risesoft.risebi.collection.service.TemplateIndicationManager;
import net.risesoft.risebi.collection.service.TemplateManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.ContextUtil;
import net.risesoft.risebi.commons.util.JSONUtil;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
*
* @author <a href="mailto: dingzhaojun2046@hotmail.com">丁兆俊</a>
* @version $Revision$
* 
*/
@Namespace("/admin/DataExchangeAction")
public class DataExchangeAction extends BaseAction {
	private static final long serialVersionUID = -8226620737253659041L;
	
	private CatalogManager catalogManager;
	private DbMetaDataManager dbMetaDataManager;
	private DynaBeanManager dynaBeanManager;
	private TemplateManager templateManager;
	private TemplateIndicationManager templateIndicationManager;
	private ReportWindowPeriodManager reportWindowPeriodManager;
	private ReportInstanceManager reportInstanceManager;
	private ReportPeriodManager reportPeriodManager;
	private ReportPeriodTypeManager reportPeriodTypeManager;
	
	private HashMap<String,Object> dynaMap;	
	private File file; //file upload
	private String fileFileName; //file upload
	private String fileContentType; //file upload
	private String query;
	
	@Action(value="prepareSarpImport",
			results={@Result(name="success", location="/admin/import/sarpImport.jsp")}
		)
	public String prepareSarpImport() throws Exception {
		try{
			List<ReportPeriodType> lst = reportPeriodTypeManager.getObjects(ReportPeriodType.class);
			this.json = JSONUtil.getJsonString("include","guid,name",lst);
		}catch(Exception e){
			this.json = "[]";
		} 

		return SUCCESS;
	}

	@Action(value="sarpImport")
	public String sarpImport() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		String err = "";
		try{
			User user = getSessionUser();
			Importer importer = (Importer) ContextUtil.getAc().getBean("sarpImporter");			
			
			ImpExpMessage impExpMessage = importer.importData(file, user.getDefaultApp().getGuid(), (String)dynaMap.get("template"), (String)dynaMap.get("reportperiod_instance"), false, false, dynaMap.get("skip").toString().startsWith("1")?true:false, user);

			ldb.set("success", true);
			ldb.set("reason", "导入报告:\n\r" + "" + impExpMessage.getSuccessfulItemCount());
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			err = err + e.getMessage() + "\r\n";
			
			ldb.set("success", false);
			ldb.set("reason", err);
			this.json = JSONObject.fromObject(ldb).toString();
		}
		
		return AJAX;
	}
	
	@Action(value="refreshJson")
	public String refreshJson() throws Exception {
		try{
			if("periodInstance".equalsIgnoreCase(this.whichJson)){
				List<ReportPeriod> lst = reportPeriodManager.find("from ReportPeriod rp where rp.reportPeriodType.guid='" + this.node + "'");
				this.json = JSONUtil.getJsonString("include", "guid,name", lst);					
			}else if("template".equalsIgnoreCase(this.whichJson)){
				List<ReportWindowPeriod> lst = reportWindowPeriodManager.getWindowReportPeriodsByPeriodId(this.node);
				ArrayList alist = new ArrayList();
				for(ReportWindowPeriod rwp : lst){
					Map map = new HashMap();
					map.put("guid", (String)rwp.getTemplate().getGuid());
					map.put("name", (String)rwp.getTemplate().getName());
					alist.add(map);
				}					
				this.json = JSONUtil.getJsonString(null, null, alist);
			}else if("tableName".equalsIgnoreCase(this.whichJson)){
				List lst = templateIndicationManager.find("select distinct t.tableName from TemplateIndication t where t.template.guid='" + this.node + "'");
				ArrayList alist = new ArrayList();
				for(Object o : lst){
					Map map = new HashMap();
					map.put("guid", (String)o);
					map.put("name", (String)o);
					alist.add(map);
				}					
				this.json = JSONUtil.getJsonString(null, null, alist);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return AJAX;
	}

	public CatalogManager getCatalogManager() {
		return catalogManager;
	}

	public void setCatalogManager(CatalogManager catalogManager) {
		this.catalogManager = catalogManager;
	}

	public DbMetaDataManager getDbMetaDataManager() {
		return dbMetaDataManager;
	}

	public void setDbMetaDataManager(DbMetaDataManager dbMetaDataManager) {
		this.dbMetaDataManager = dbMetaDataManager;
	}

	public DynaBeanManager getDynaBeanManager() {
		return dynaBeanManager;
	}

	public void setDynaBeanManager(DynaBeanManager dynaBeanManager) {
		this.dynaBeanManager = dynaBeanManager;
	}

	public TemplateManager getTemplateManager() {
		return templateManager;
	}

	public void setTemplateManager(TemplateManager templateManager) {
		this.templateManager = templateManager;
	}

	public TemplateIndicationManager getTemplateIndicationManager() {
		return templateIndicationManager;
	}

	public void setTemplateIndicationManager(
			TemplateIndicationManager templateIndicationManager) {
		this.templateIndicationManager = templateIndicationManager;
	}

	public ReportWindowPeriodManager getReportWindowPeriodManager() {
		return reportWindowPeriodManager;
	}

	public void setReportWindowPeriodManager(
			ReportWindowPeriodManager reportWindowPeriodManager) {
		this.reportWindowPeriodManager = reportWindowPeriodManager;
	}

	public ReportInstanceManager getReportInstanceManager() {
		return reportInstanceManager;
	}

	public void setReportInstanceManager(ReportInstanceManager reportInstanceManager) {
		this.reportInstanceManager = reportInstanceManager;
	}

	public ReportPeriodManager getReportPeriodManager() {
		return reportPeriodManager;
	}

	public void setReportPeriodManager(ReportPeriodManager reportPeriodManager) {
		this.reportPeriodManager = reportPeriodManager;
	}

	public ReportPeriodTypeManager getReportPeriodTypeManager() {
		return reportPeriodTypeManager;
	}

	public void setReportPeriodTypeManager(
			ReportPeriodTypeManager reportPeriodTypeManager) {
		this.reportPeriodTypeManager = reportPeriodTypeManager;
	}

	public HashMap<String, Object> getDynaMap() {
		return dynaMap;
	}

	public void setDynaMap(HashMap<String, Object> dynaMap) {
		this.dynaMap = dynaMap;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}		

}
